package com.vonchenchen.renderer;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

/**
 * Created by  on 2021/1/27.
 */
public class VideoCaptureFactory {
    private static final String TAG = "CAM-FACTORY";

    public VideoCaptureFactory() {
    }

    private static boolean isLReleaseOrLater() {
        if ("ocean".equalsIgnoreCase(Build.DEVICE) && "oe106".equalsIgnoreCase(Build.MODEL)) {
            return false;
        } else if ("trident".equalsIgnoreCase(Build.DEVICE) && "de106".equalsIgnoreCase(Build.MODEL)) {
            return false;
        } else if ("shark".equalsIgnoreCase(Build.DEVICE) && "skr-a0".equalsIgnoreCase(Build.MODEL)) {
            return false;
        } else if ("hnnem-h".equalsIgnoreCase(Build.DEVICE)) {
            return false;
        } else if ("on7xelte".equals(Build.DEVICE) && "SM-G610F".equals(Build.MODEL)) {
            return false;
        } else if (!"m2c".equals(Build.DEVICE) && !"M578CA".equals(Build.MODEL)) {
            if (!"samsung".equalsIgnoreCase(Build.MANUFACTURER) || Build.MODEL == null || !Build.MODEL.contains("SM-G930") && !Build.MODEL.contains("SM-G935") && !Build.MODEL.contains("SM-G950") && !Build.MODEL.contains("SM-G955") && !"SC-02H".equals(Build.MODEL) && !"SCV33".equals(Build.MODEL) && !"SC-02J".equals(Build.MODEL) && !"SCV36".equals(Build.MODEL) && !"SM-G892A".equals(Build.MODEL) && !"SM-G892U".equals(Build.MODEL) && !"SC-03J".equals(Build.MODEL) && !"SCV35".equals(Build.MODEL)) {
                if ("oneplus".equalsIgnoreCase(Build.MANUFACTURER)) {
                    return false;
                } else {
                    return Build.VERSION.SDK_INT >= 21;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static VideoCapture createVideoCapture(int id, Context context, long nativeVideoCaptureDeviceAndroid) {
        return (VideoCapture)(isLReleaseOrLater() && !VideoCaptureCamera2.isLegacyDevice(context, id) ? new VideoCaptureCamera2(context, id, nativeVideoCaptureDeviceAndroid) : new VideoCaptureCamera(context, id, nativeVideoCaptureDeviceAndroid));
    }

    public static int getNumberOfCameras(Context appContext) {
        return VideoCaptureFactory.AndroidCameraInfo.getNumberOfCameras(appContext);
    }

    public static String getDeviceName(int id, Context appContext) {
        return isLReleaseOrLater() && !VideoCaptureCamera2.isLegacyDevice(appContext, id) ? VideoCaptureCamera2.getName(id, appContext) : VideoCaptureCamera.getName(id);
    }

    public static int getDeviceOrientation(int id, Context appContext) {
        return isLReleaseOrLater() && !VideoCaptureCamera2.isLegacyDevice(appContext, id) ? VideoCaptureCamera2.getSensorOrientation(id, appContext) : VideoCaptureCamera.getSensorOrientation(id);
    }

    public static String getCapabilities(int id, Context appContext) {
        String cap = null;
        if (isLReleaseOrLater() && !VideoCaptureCamera2.isLegacyDevice(appContext, id)) {
            cap = VideoCapture.fetchCapability(id, appContext, VideoCaptureCamera2.getCaptureName());
        } else {
            cap = VideoCapture.fetchCapability(id, appContext, VideoCaptureCamera.getCaptureName());
        }

        if (cap == null) {
            Log.e("CAM-FACTORY", "Capability hasn't been created");
        } else {
            printCameraInfo(cap);
        }

        return cap;
    }

    public static int printCameraInfo(String cap) {
        int bytesPerLoop = 150;
        int loopTimes = cap.length() / bytesPerLoop + 1;

        for(int i = 0; i < loopTimes; ++i) {
            try {
                String cameraInfo = "lines = " + loopTimes + ":";
                if (i == loopTimes - 1) {
                    cameraInfo = cameraInfo + cap.substring(i * bytesPerLoop, cap.length());
                } else {
                    cameraInfo = cameraInfo + cap.substring(i * bytesPerLoop, (i + 1) * bytesPerLoop);
                }

                Log.d("CameraInfo", cameraInfo);
            } catch (IndexOutOfBoundsException var5) {
                var5.printStackTrace();
            }
        }

        return 0;
    }

    static class AndroidCameraInfo {
        private static int sNumberOfSystemCameras = -1;

        AndroidCameraInfo() {
        }

        private static int getNumberOfCameras(Context appContext) {
            if (sNumberOfSystemCameras == -1) {
                if (Build.VERSION.SDK_INT < 23 && appContext.getPackageManager().checkPermission("android.permission.CAMERA", appContext.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
                    sNumberOfSystemCameras = 0;
                    Log.e("CAM-FACTORY", "Missing android.permission.CAMERA permission, no system camera available");
                } else {
                    sNumberOfSystemCameras = 0;
                    if (VideoCaptureFactory.isLReleaseOrLater()) {
                        sNumberOfSystemCameras = VideoCaptureCamera2.getNumberOfCameras(appContext);
                    }

                    if (sNumberOfSystemCameras == 0) {
                        sNumberOfSystemCameras = VideoCaptureCamera.getNumberOfCameras();
                    }
                }
            }

            return sNumberOfSystemCameras;
        }
    }
}
